<%
 @article.body= @article.body+"<br/>Original source "+link_to(params[:url],params[:url]) unless params[:url].nil?
 imgcount=0 
 firstimg=''
%>
<script language="JavaScript">
<% if( @images || (@images.length)>0) %>
var imgs=[ 
<% 0.upto(@images.length-1){|i|
  if !(@images[i].blank?) %>
	<% if imgcount>0 %>
	,
	<% else %>
	<% firstimg=@images[i] %>
	<% end %>
	'<%=h @images[i] %>'
	<% imgcount=imgcount+1 %>
<% end %>
<% } %>
];
function change_img()
{
document.getElementById("img_index").src=imgs[document.getElementById("img_list").value];
document.getElementById("media_url").value=imgs[document.getElementById("img_list").value];
}
<% end %>
function add_tag()
{
  var a=prompt('Please enter a tag name','');
  if(a)
  {
    a=a.strip()
    if(a.length>0)
    {
    	for(i=0;i<$('article_tag_id').length;++i)
    		if($('article_tag_id').options[i].text==a)
    		{
	    		$('article_tag_id').value=$('article_tag_id').options[i].value;
    			return false;
    		}
    	$('tagname').value=a;
    	return true;
    }
  }
  return false;
}

</script>
<div id=subform><input id=tagname name=tagname type=hidden value=''></input></div>

<% form_for :article, 
            :url => {:controller =>'articles',:action => :create}, 
            :html => { :method => :post } do |f| -%>
             <%= f.hidden_field :person_id %>
  <p>Title:<br /><%= f.text_field :title, :size => 60 %></p>
  <p>Synopsis:<br /><%= f.text_area :synopsis, :rows => 8, :cols => 90 %></p>
  <p>Body:<br /><%= f.text_area :body, :rows => 40, :cols => 90 %></p>
  <%- if logged_in? && (current_person.admin?) -%>
  <p>Category:<br />
  		    <%= f.collection_select :category_id, Category.find(:all), :id, :name, :include_blank => false %> 
  <p>Category 2:<br />
  		    <%= f.collection_select :categoryt_id, Categoryt.find(:all), :id, :name, :include_blank => true %> 
  <%- end -%>
  <p>Tag:<br />
  		    <%= f.collection_select :tag_id, Tag.find(:all), :id, :name, :include_blank => true %><%= link_to_remote "Add a tag", 
       :url => {:controller=>'tags',:action => "add"},:method=>:get,:submit   => "subform",:condition =>"add_tag()"  %></p>
     <% if logged_in? && current_person.admin?%>        
  <p>Published? <%= f.check_box :published %></p>
    <%end%>       
    <p>Media file:<br /><%= f.text_field :media_url, :size => 60,:id=>'media_url' %></p>
			<% if( imgcount>0) %>
			<br/> choose a picture
			<select id='img_list' name="img_list" onChange="change_img()" >
			<% 0.upto(imgcount-1){|i| %>
    		<option value="<%= i%>" <%= i==0?'selected="selected"':'' %>><%= i %></option>
    		<% } %>
  			</select>
			<br/>
			<img id='img_index' src='<%=h firstimg%>'></img>
			<% end %>
  		    <%= submit_tag 'Save' %> 
<% end %>